package cn.hg.jbsa.config;

import com.jfinal.core.Controller;
import com.jfinal.render.Render;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import io.jboot.web.controller.JbootControllerContext;
import io.jboot.web.render.JbootErrorRender;
import io.jboot.web.render.JbootRenderFactory;

/**
 * @author 胡高
 * @date 2022/01/23
 */
public class JbsaRenderFactory extends JbootRenderFactory {
    /* (non-Javadoc)
     * @see io.jboot.web.render.JbootRenderFactory#getErrorRender(int)
     */
    @Override
    public Render getErrorRender(int errorCode) {
        Controller controller = JbootControllerContext.get();

        if (ObjectUtil.isNull(controller)) {
            return this.getNullRender();
        }

        String path = controller.getControllerPath();

        return this.getRender(path, errorCode, null);
    }

    /* (non-Javadoc)
     * @see io.jboot.web.render.JbootRenderFactory#getErrorRender(int, java.lang.String)
     */
    @Override
    public Render getErrorRender(int errorCode, String view) {
        Controller controller = JbootControllerContext.get();

        if (ObjectUtil.isNull(controller)) {
            return this.getNullRender();
        }

        return this.getRender(controller.getControllerPath(), errorCode, view);
    }

    private Render getRender(String path, int errorCode, String view) {
        return CharSequenceUtil.isBlank(view) ? new JbootErrorRender(errorCode, this.constants.getErrorView(errorCode))
            : new JbootErrorRender(errorCode, view);
    }
}
